package com.hyving.summer.excelparse.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.alibaba.fastjson.JSON;
import com.hyving.summer.excelparse.entity.GoodsCount;
import com.hyving.summer.excelparse.entity.SourceOrder;
import lombok.SneakyThrows;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

public class ExcelController {

    @SneakyThrows
    public static void main(String[] args) {

        String filePath ="C:\\Users\\Administrator\\Desktop\\1.xlsx";

//        String currentPath = new File(ExcelController.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent();
//        // 在当前目录下寻找文件，例如文件名为"example.txt"
//        String filePath = currentPath + File.separator + "example.txt";



        File f =new File(filePath);

        InputStream fileInputStream = new FileInputStream(filePath);

        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
//        params.setSaveUrl("/excel/");
//        params.setNeedSave(false);
        List<SourceOrder> orders = ExcelImportUtil.importExcel(fileInputStream, SourceOrder.class, params);
        System.out.println(JSON.toJSON(orders));
        System.out.println(orders.size());


        List<SourceOrder> cancelOrders=new ArrayList<>();
        List<SourceOrder> finishOrders=new ArrayList<>();
        List<SourceOrder> refundOrders=new ArrayList<>();

        HashMap<String , GoodsCount> goodsMap =new HashMap<>();

        for(int i =0;i<orders.size();i++){
            SourceOrder s =orders.get(i);
            String goodsName =s.getGoodsName();
            String[] g =goodsName.split(" ");
            String name =g[0];
            GoodsCount goodsCount = goodsMap.get(name);
            if(goodsCount==null){
                goodsCount =new GoodsCount();
                goodsCount.setName(name);
            }


            if("已取消".equals(s.getOrderStatus())
            ||"待配货".equals(s.getOrderStatus())
            ||"已发货未签收".equals(s.getOrderStatus())){


            }else{
                if("售后完成".equals(s.getSaleStatus())||"售后处理中".equals(s.getSaleStatus())){
                    goodsCount.refund();
                }else{
                    goodsCount.sale();
                }


            }


            goodsMap.remove(name);
            goodsMap.put(name,goodsCount);

        }

        Set<String> keySet = goodsMap.keySet();
        List<GoodsCount> gg =new ArrayList<>();
        for (String key : keySet) {
            gg.add(goodsMap.get(key));

        }
        gg =gg.stream().sorted(Comparator.comparing(GoodsCount::getSaleCount,Comparator.reverseOrder())).collect(Collectors.toList());

        for(GoodsCount g:gg){
            System.out.println("商品名称:" + g.getName() + " , " +
                    " 总订单数量: " + g.getSaleCount()+
                    " 退款数量： " + g.getRefundCount()+
                    " 退货率： "+g.countRefund()+"%");

        }

    }





}
